<template>
<div class="cmt-container">
    <hr>
    <textarea placeholder="请输入评论内容" maxlength="120" v-model="msg"></textarea>
    <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button> 

    <div class="cmt-list">
        <div class="cmt-item" v-for="(item,i) in comments" :key=i>
            <div class="cmt-title">
                第{{i+1}}楼&nbsp;&nbsp;用户：匿名用户&nbsp;&nbsp;发表时间：{{item.date|dateFormat}}
            </div>
            <div class="cmt-body">
                {{item.msg}}
            </div>
        </div>

    </div>
    <mt-button type="danger" size="large" plain>加载更多</mt-button> 
</div>
</template>



<script>
export default {
    data(){
        return {
            msg:'', //评论输入的内容
            date:'',//评论时间
            comments:[
            ]
        }
    },
    created(){
    this.loadComments();
    },
    methods:{
        postComment(){
        //静态评论
        //    this.comments.push({msg:this.msg})
        //    this.msg=''

        //json数据评论
        var comment={msg:this.msg,date:new Date()}
		//从localStorage中获取所有评论
		var list=JSON.parse(localStorage.getItem('cmts')||'[]')
		list.unshift(comment)
		localStorage.setItem('cmts',JSON.stringify(list))
        this.msg=''
        this.loadComments();
        },
        loadComments(){//从本地的localStorage中，加载评论列表
		var list=JSON.parse(localStorage.getItem('cmts')||'[]')
		this.comments=list	
		}
    }
}
</script>



<style lang="scss" scoped>
.cmt-container{
    h3{
        font-size: 18px;
    }
    textarea{
        font-size: 14px;
        height: 85px;
        margin: 0;
    }
    .cmt-list{
        margin: 5px 0;
        .cmt-item{
            font-size: 13px;
            .cmt-title{
                line-height: 30px;
                background-color: #ccc;
            }
            .cmt-body{
                line-height: 35px;
                text-indent: 2em;
            }
        }

    }
}
</style>


